package com.atguigu.datastream.day03;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


/**
 * ClassName: Flink_Source_Kafka
 * Package: com.atguigu.day03
 * Description:
 *                     1.14之后
 *                      新版本
 * @Author ChenJun
 * @Create 2023/4/8 16:40
 * @Version 1.0
 */
public class Flink_04_Source_Kafka_new {
    public static void main(String[] args) throws Exception {

        //1. 获取flink 的流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //2. 设置并行度
        env.setParallelism(1);

        //3. 读取kafka数据
        //TODO 从Kafka读取数据 1.13版本后写法
        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers("hadoop102:9092")
                .setGroupId("221109")
                .setTopics("first")
                .setStartingOffsets(OffsetsInitializer.latest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();
        DataStreamSource<String> streamSource = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafkaSource");


        // 打印
        streamSource.print();

        //执行
        env.execute();


    }
}
